<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Overtrue\Wechat\Server;
use Overtrue\Wechat\Message;
use Overtrue\Wechat\User as UserService;
use Overtrue\Wechat\QRCode;
use App\User;
use DB;

class WxController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        define('Token','okcokkjdfjakjfadjjfaljd');
        $server = new Server('wx07d5b5ed27022a37','okcokkjdfjakjfadjjfaljd',null);
        $server->on('event','subscribe',[$this,'guanzhu']);
        
       // $server->on('event','unsubscribe', function($event){
         //  DB:table('users')->where('openid',$event->FromUserName)->update(['state'=>0]);
      // });
       $server->on('event' , 'unsubscribe' , function($event) {
        DB::table('users')->where('openid',$event->FromUserName)->update(['state'=>0]); 
      });
        return $server->serve();
    }

     public function guanzhu($event){
      //  $user = new User();
      // $appId  = 'wx07d5b5ed27022a37';
      // $secret = '551a1aa50a2c0cebc132385f570233d4';

       
	//$a = $event->FromUserName;
	//$userService = new User($appId, $secret);
 $wxuser = ( new UserService( env('WX_ID') , env('WX_SEC')  ) )->get($event->FromUserName);
      // $wxuser =  new User($appId,$secret);
      // $wxuser->get($event->FromUserName);
      //第一种，openid已存在，且state=1,不做处理，如果state=1,则改state=0;      //第二种，openid不存在，且二维码有参数
      //第三种，openid不存在，且二维码没有参数，作为一级代理商  
      $user = User::where('openid',$event->FromUserName)->first();

      if($user && $user->state==0){
         $user->state = 1 ;
         $user->save();
         return Message::make('text')->content("欢迎订阅");
       }else if($user && $user->state==1){
         return;
       }

       $user = new User();

       if($event->EventKey){
          $puid = str_replace('qrscene_', '' ,$event->EventKey );
          $pu = DB::table('users')->where('uid',$puid)->first();

          $user->p1 = $puid;
          $user->p2 = $pu->p1;
          $user->p3 = $pu->p2;
        }


        $user->openid = $event->FromUserName;
        $user->subtime = time();
        $user->name = $wxuser->nickname;
       // $user->name = $wxuser->nickname;
        $user->save();
        
        //根据用户id生成场景二维码
        $this->qr($user->uid);       
 
        return Message::make('text')->content("欢迎订阅");
   }
   protected function qr($uid){
        $qr = new QRCode(env('WX_ID'),env('WX_SEC'));
        $result = $qr->forever($uid);//生成永久二维码
        $today = $this->mkd();
        $qing = 'qr_'. $uid .'.jpg';
 
        $qr->download($result->ticket, public_path() . $today .'/'. $qing);
        
        return $today. '/'.$qing;
    }
     
  protected  function mkd(){
    $pub = public_path();
    $today = date('/Y/m');

    if(!is_dir($pub.$today)){
      mkdir($pub.$today,0777,true);
      }
     return $today;
    }

}
